KEY = require('fckoa/db/redis/verify_key')  "auth/password/reset:mail"
USER = require './_model/user'
CHECK = require './_create_check'
# if RE.MAIL.test(account)
#        pos = 1
#    else if RE.PHONE.test(account)

TABLE = {
    MAIL : 1
    PHONE : 2
}

module.exports = require('fckoa/router') {
get:{
    "set-:key":(ctx)->
        {key} = ctx.params
        r = await KEY.get(key)
        if r
            await KEY.renew(key)
        else
            r = ''
        ctx.body = r
}
post:{
    set:(ctx)->
        {key, password} = ctx.request.body
        raise password:await CHECK.password(password)

        user_id = await USER.id_by_mail(await KEY.get(key))
        await USER.password(user_id, password)
        await KEY.del(key)
        ctx.body = 0

    reset:(ctx)->
        await require('./_captcha')(ctx)
        {account, host} = ctx.request.body
        [user_id, table_id, xid] = await require('./_account')(account)
        switch table_id
            when TABLE.MAIL
                key = await KEY.new(account)
                await require('fckoa/yun/smtp') {
                    to:account
                    from_name:host
                    subject:"[#{host}] 密码重置"
                    text:"""\
请点击下面的链接完成密码重置（若链接无法点击，请复制到浏览器地址栏）。

#{ctx.request.protocol}://#{host}/auth/password/set-#{await KEY.new(account)}

此链接将在二十四小时后过期失效。"""
                }
            when TABLE.PHONE
                console.log 'phone', account
        ctx.body = table_id
}
}
